home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: cix.compulink.co.uk!usenet
- From: alexbrn@cix.compulink.co.uk ("Alexander Brown")
- Subject: Object type v object mode
- Message-ID: <DM1JIv.25A@cix.compulink.co.uk>
- Organization: Compulink Information eXchange
- Date: Wed, 31 Jan 1996 10:23:18 GMT
- X-News-Software: Ameol
-
-
- I'm trying to devise some guidelines for myself on when it's appropriate
- to model different behaviours of something being modelled by having many
- multiple types, when by having different "modes" of behaviour in a
- smaller number of types.
-
- For example, consider an animation class that inherits from a container
- containing animation frames. The class has Play and Stop methods which
- control the animation, and inherits methods like Add and Remove from the
- container, for editing its frame sequence.
-
- Now, let's say the animation class should also be able to play in
- reverse. To add this functionality we _could_ design multiple types
-
-
- / ForwardAnimation
- container ---- AbstAnimation /
- \
- \ ReverseAnimation
-
- AbstAnimation might have a pure virtual method Play which was implemented
- in the subclasses to provide forwards or reverse playback as appropriate.
-
- I'd guess however that nobody would want to do it this way (?), but would
- instead add a data member to the single Animation class together with
- methods like SetDirection and GetDirection which would allow the object's
- user to control replay direction.
-
- Okay, now let's say we also want to have the ability to have read-only
- animations -- they're passed a file spec in their constructor, and if
- they load their frame sequence successfully, they can be played and
- stopped, but not edited.
-
- Again we're faced with the same choice: make new types (e.g.,
- ReadOnlyAnimation and EditableAnimation) or add a data member to the
- animation class which allows the object's user to set a read-only mode.
-
- If we take the former approach, the ReadOnlyAnimation class could use
- private or protected inheritance of the container to hide the container's
- methods like Add and Remove, since these would not make sense in a
- read-only animation.
-
- In the latter approach - of adding a new read-only "mode" to our
- Animation class - the problem arises: what to do when the class's Add or
- Remove methods are called? throw an exception?
-
- (Another approach might be a class relationship like:
-
- container ---[private inh]--- ReadOnlyAnimation ---[pub. inh]--- Animation
-
- with the final Animation class exposing some of the container's methods
- by calling them through.
-
- Another might be to use a notion of constness?)
-
- Anyhow, my question at the end of all this is: _when_ to represent
- different behaviour by a different type, and _when_ by a different "mode"?
-
- Either extreme would be bad. Too many types would lead to an
- over-dispersed object model (IMO). Too many "modes" would move us away
- from OO towards having procedural programming dressed-up as classes.
-
- As a working rule of thumb I think that a class can have "modes" so long
- as all of its services remain reconcilable with each mode.
-
- What do other people think ?
-
-
- --
- Alex Brown | "I wasted time, and now
- alexbrn@cix.compulink.co.uk | time doth waste me."
- http://www.compulink.co.uk/~arachne/ |
-